import Request from 'luch-request'
import {
    RouterMount
} from '../router'

let env = 'https://api.hszjcy.com/' //正式
let dev = 'https://api.hszjcy.com' //测试

const http = new Request()
let baseURL = ''
if (process.env.VUE_APP_ENV == 'test') {
    baseURL = 'https://api.hszjcy.com'
} else if (process.env.VUE_APP_ENV == 'demo') {
    baseURL = 'https://api.hszjcy.com'
} else if (process.env.VUE_APP_ENV == 'dev') {
    baseURL = 'https://api.hszjcy.com'
} else if (process.env.VUE_APP_ENV == 'prod') {
    baseURL = 'https://api.hszjcy.com/'
} else {
    baseURL = dev
}


http.setConfig((config) => {
    config.baseURL = baseURL
    config.header = {
        ...config.header,

    }
    return config
})

//请求之前拦截器
http.interceptors.request.use((config) => {
    uni.getNetworkType({
        success: (res) => {
            if (res.networkType == 'none') {
                uni.showModal({
                    title: '没有网络',
                    content: '请检查网络设置',
                    showCancel: false,
                    success: (res) => {
                        if (res.confirm) {
                            config.header = {
                                ...config.header,
                                token: uni.getStorageSync('ims-token'),
                                companyId: uni.getStorageSync('userInfo').companyId,
                            }
                        }
                    }
                })
            }
        }
    })
    config.header = {
        ...config.header,
        token: uni.getStorageSync('ims-token'),
        companyId: uni.getStorageSync('userInfo').companyId,
        orgId: uni.getStorageSync('userInfo').companyId
    }
    return config
}, (config) => {
    return Promise.reject(config)
})

//响应拦截
http.interceptors.response.use((response) => {
    if (response.data.code == '10201') {
        uni.reLaunch({
            url: '/userPages/pages/login/login'
        })
    }
    return response.data
})

export {
    http
}